<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
         <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<style type="text/css">
table {
	width: 800px;
	border-collapse: collapse;	
}

td,th {
	padding: 8px 15px;
	text-align: center;
}
</style>

<p>
	<button type="button" onclick="add()">新增</button> 
	<button type="button" onclick="search()">查询</button>
</p>
<table border="1">
	<thead>
		<th>序号</th>
		<th>班级名</th>
		<th>班主任</th>
		<th colspan="2">操作</th>
	</thead>
	<tbody id="tbody"></tbody>
</table>


<script type="text/javascript">
var queryParam="";

$(function(){
	$('#dialog').dialog({
		autoOpen:false,
		title:'编辑',
		modal:true,
		width:400,
		buttons:[{
			text:'提交',
			click : function(){
				var self=$(this);
				var form=$(this).find('form');
				var params=form.serialize();
				var action=form.attr('action');
			
				if(action=='tbClass/save'){
					$.post(action,params,function(data){
						refreshTable();
						self.dialog('close');
					})
				}else{
					queryParam=params;
					refreshTable();
					self.dialog('close');
				}
			}
		},{
			text :'退出',
			click : function(){
				$(this).dialog('close');
			}
		}]
	})
	refreshTable();
})

function refreshTable(){
	$.post('tbClass/list',queryParam,function(data){
		var tbody=$('#tbody');
		tbody.empty();
		for(var i=0;i<data.length;i++){
			var bj=data[i];
			var tr=$('<tr/>');
			tr.append($('<td/>').html(bj.id));
			tr.append($('<td/>').html(bj.name));
			tr.append($('<td/>').html(bj.manager));
			tr.append($('<td/>').append($('<button/>').html('修改').attr('onclick','xg('+bj.id+')')));
			tr.append($('<td/>').append($('<button/>').html('删除').attr('onclick','del('+bj.id+')')));
			tbody.append(tr);
		}
		$('button').button();
	})
}

function xg(id){
	var dialog=$('#dialog');
	dialog.load('tbClass/edit',{id:id},function(){
		dialog.dialog('open');
	})
}
function add(){
	var dialog=$('#dialog');
	dialog.load('tbClass/edit',function(){
		dialog.dialog('open');
	})
}

function search(){
	var dialog=$('#dialog');
	dialog.load('tbClass/search',queryParam,function(){
		dialog.dialog('open');
	});
}
function del(id){
	if(confirm('是否要删除这条数据？')){
		$.post('tbClass/delete',{id:id},function(data){
			refreshTable();
		})
	}
}
</script>
